﻿using System;
using DMS.BaseClass;
using DMS.Business.Modules.Knowledge;
using DMS.Business.Modules.Organization;
using DMS.Business.Utility;
using System.Configuration;

namespace DMS.Web.Modules.Knowledge 
{
    public partial class KnowledgeEdit : System.Web.UI.Page
    {
        public string serverPath = ConfigurationManager.AppSettings["FileServer"];
        string initContent = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mode = Request["mode"];
                string id = Request["id"];
                if (!String.IsNullOrEmpty(mode) && "view".Equals(mode))
                {
                    ClientHelper.Instance.RenderScript(Page, null, "editor.config.disableReadonlyStyling = true;$('#imgBtn').Css('display', 'none')");
                    Title.ReadOnly = true;
                    Summary.ReadOnly = true;
                    PublishTime.CssClass = "";
                    btnSave.Visible = false;
                }

                if (!String.IsNullOrEmpty(id))
                {
                    CommonKnowledge knowledge = KnowledgeService.GetKnowledge(id);
                    PublishTime.Text = knowledge.PublishTime.GetDateTimeFormats('g')[0];
                    Title.Text = knowledge.Title;
                    Summary.Text = knowledge.Summary;
                    ContentValue.Value = knowledge.Content;
                    ClientHelper.Instance.RenderScript(Page, "", "init();");
                }
                else
                {
                    PublishTime.Text = DateTime.Now.GetDateTimeFormats('g')[0];
                }
            }
        }

        //保存项目信息
        public void SaveForm(object sender, EventArgs e)
        {
            string id = Request["id"];
            CommonKnowledge knowledge = new CommonKnowledge();
            knowledge.Title = Title.Text;
            knowledge.Summary = Summary.Text;
            knowledge.PictureUrl = "";
            knowledge.PublishTime = DateTime.Now;
            knowledge.IsDeleted = false;
           
            User user = UserHelper.GetContextUser();
            knowledge.Publisher = user.Name;
            knowledge.PublisherID = user.ID.ToString();
            knowledge.Content = ContentValue.Value;
            if(String.IsNullOrEmpty(id)){
                KnowledgeService.SaveKnowledge(knowledge);
            }else{
                knowledge.KnowledgeID = new Guid(id).ToString();
                KnowledgeService.UpdateKnowledge(knowledge);
            }
            ClientHelper.Instance.RenderScript(Page, "", "window.opener.location = window.opener.location");
            ClientHelper.Instance.CloseWindow("保存成功", "true", RefreshType.NoRefresh);
        }
    }
}